/*
 * Copyright (C) 2007 Evan Long    longx223 (at) umn (dot) edu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/**
 * @file   gesture_recognition.c
 * @author Evan Long
 * 
 * @brief  Implementation of gesture recognition functions.
 * 
 * 
 */

#include <math.h>
#include <stdio.h>
#include <string.h>
#include "gesture_recognition.h"
#include "safe_malloc.h"

char get_dir(zone_t* z)
{
  if(z->up >= z->down && z->up >= z->left && z->up >= z->right)
  {
    return 'u';
  }
  else if(z->down >= z->up && z->down >= z->left && z->down >= z->right)
  {
    return 'd';
  }
  else if(z->left >= z->up && z->left >= z->down && z->left >= z->right)
  {
    return 'l';
  }
  else
  {
    return 'r';
  }
}

void pick_dir(zone_t* z, point_t* p1, point_t* p2, int dir)
{
  if(dir == VERTICAL)
  {
    if(p1->y < p2->y)
    {
      z->down++;
    }
    else
    {
      z->up++;
    }
  }
  else if(dir == HORIZONTAL)
  {
    if(p1->x < p2->x)
    {
      z->right++;
    }
    else
    {
      z->left++;
    }
  }
}

char* detect_gesture(cvector_t* points)
{
  zone_t z[4];
  memset(z, 0, 4 * sizeof(zone_t));

  int i;
  for(i=0; i<points->end_index-1; i++)
  {
    point_t* p1 = (point_t*)element_at(points, i);
    point_t* p2;
    /*only want to measure slopes on points at least 10 units apart*/
    int j;
    for(j=i+1; j<points->end_index; j++)
    {
      /*the last few data points might not have 10 units between them*/
      p2 = (point_t*)element_at(points, j);
      int dist = (int)(sqrt(pow(p1->x-p2->x, 2) + pow(p1->y-p2->y, 2)));
      if(dist >= POINT_DIST)
      {
	break;
      }
    }

    /*might be the source of the floating point dump*/
    double slopes = ( (180.0 / PI) * atan( 
			(double)(p1->y-p2->y)/(double)(p1->x-p2->x) ) );
    if((slopes >= 75 && slopes <= 90) || (slopes >= -90 && slopes <= -75))
    {
      z[0].order += i;
      z[0].total_points++;
      pick_dir(&z[0], p1, p2, VERTICAL);
    }
    else if(slopes > -75 && slopes <= -15)
    {
      z[1].order += i;
      z[1].total_points++;
      pick_dir(&z[1], p1, p2, HORIZONTAL);
    }
    else if(slopes > -15 && slopes <= 15)
    {
      z[2].order += i;
      z[2].total_points++;
      pick_dir(&z[2], p1, p2, HORIZONTAL);
    }
    else if(slopes > 15 && slopes < 75)
    {
      z[3].order += i;
      z[3].total_points++;
      pick_dir(&z[3], p1, p2, HORIZONTAL);
    }
  }

  int max = -1;
  int lzone1 = -1;
  int lzone2 = -1;
  for(i=0; i<4; i++)
  {
    if(z[i].total_points > max)
    {
      max = z[i].total_points;
      lzone1 = i;
    }
  }

  int smax = -1;
  for(i=0; i<4; i++)
  {
    if(i == lzone1) continue;

    if(z[i].total_points > smax)
    {
      smax = z[i].total_points;
      lzone2 = i;
    }
  }

  if(lzone1 == -1) return NULL;

  char* cmd = (char*)safe_malloc(CMD_SIZE);
  memset(cmd, 0, CMD_SIZE);
  if(smax <= (.25) * (double)max)
  {
    sprintf(cmd, "%d%c%d%d", lzone1, get_dir(&z[lzone1]), 9, 9);
  }
  else
  {
    int first;
    int second;
    if(MIN(z[lzone1].order, z[lzone2].order) == z[lzone1].order)
    {
      first = lzone1;
      second = lzone2;
    }
    else
    {
      first = lzone2;
      second = lzone1;
    }
    sprintf(cmd, "%d%c%d%c", first, get_dir(&z[first]), 
	    second, get_dir(&z[second]));
  }
  return cmd;
}
